package cn.lele.reggie.service.impl;

import cn.lele.reggie.common.BaseContext;
import cn.lele.reggie.common.R;
import cn.lele.reggie.entity.ShoppingCart;
import cn.lele.reggie.mapper.ShoppingCartMapper;
import cn.lele.reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public R<String> addToShoppingCart(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Long dishId=shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);
        wrapper.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        ShoppingCart one = this.getOne(wrapper);
        if(one!=null){
            shoppingCart.setId(one.getId());
            shoppingCart.setNumber(one.getNumber()+1);
            this.updateById(shoppingCart);
        }else {
            this.save(shoppingCart);
        }
        return R.success("菜品或套餐加一到购物车成功");
    }
}
